package com.ssps.back.platform.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssps.back.platform.service.UserServcie;
import com.ssps.entity.User;
import com.ssps.utils.MyLog;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author li
 * @time 2020/07/21 20:01:41
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserServcie userServcie;

    @RequestMapping("userList")
    public String userList( ){
        return "user/userList";
    }
    @RequestMapping("updatePwd")
    public String updatePwd( ){
        return "user/updatePwd";
    }

    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> userList(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize ,String status,String acount) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<User> userList= userServcie.findAll(status,acount);
            PageInfo<User> pageInfo = new PageInfo<User>(userList);
            map.put("pageInfo",pageInfo);
            map.put("userList",userList);
            map.put("result", true);
        }catch(Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }
        return map;
    }

    /*
     *新增平台工作账户
     * 一键新增
     * 不需要注册
     *
     */
    @RequestMapping("saveUser")
    @ResponseBody
    public Map<String,Object> saveUser(String  acount,Integer rid,String phone){
        System.out.println(acount);
        User newUser= new User();
        String s="123";
        String salt ="aa";
        SimpleHash sh=new SimpleHash("MD5",s,salt,1024);
        String hex=sh.toHex();
        newUser.setPwd(hex);
        newUser.setStatus("正常");
        newUser.setSalt(salt);
        newUser.setAcount(acount);
        newUser.setPhone(phone);
        Map<String, Object> map=new HashMap<>();
        try{
            userServcie.saveUser(newUser,rid);
            map.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }

    /*
     *Audit
     * 对用户的信息进行审核
     * 只有通过审核的信息才可以进行操作
     *
     */

    @RequestMapping("updateAudit")
    @ResponseBody
    public Map<String,Object> updateUserAudit(User user){
        System.out.println(user.toString());
            User user1=new User();
            user1.setStatus(user.getStatus());
        user1.setUid(user.getUid());
        Map<String, Object> map=new HashMap<>();
        try{
            userServcie.updateUser(user1);
            map.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }
        return map;

    }

    /*
    *Freeze
    * 在投诉过多的情况下
    * 可以冻结改账户
    *
     */
    @RequestMapping("updateFreeze")
    @ResponseBody
    public Map<String,Object> updateUserFreeze(User user){

        Map<String, Object> map=new HashMap<>();
        try{
            userServcie.updateUser(user);
            map.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }
        return map;

    }

    /*
     *Password
     * 重置密码
     * 当后台人员忘记自己账户密码是
     * 可以通过管理员权限来重置密码
     *
     */
    @RequestMapping("resetPassword")
    @ResponseBody
    public Map<String,Object> updateUserReset(Integer uid){

        User newUser=new User();
        newUser.setUid(uid);
        String s="123";
        String salt ="aa";
        SimpleHash sh=new SimpleHash("MD5",s,salt,1024);
        String hex=sh.toHex();
        newUser.setSalt(salt);
        newUser.setPwd(hex);
        Map<String, Object> map=new HashMap<>();
        try{
            userServcie.updateUser(newUser);
            map.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }
        return map;

    }

    /*
     *Password
     * 修改密码
     * 当后台人员忘记自己账户密码是
     * 可以通过管理员权限来重置密码
     *
     */
    @MyLog
    @RequestMapping("updatePassword")
    @ResponseBody
    public Map<String,Object> updateUserPassword(HttpSession session,String oldPwd , String newPwd){
        Map<String, Object> map=new HashMap<>();
        User user = (User) session.getAttribute("loginUser");
        String salt ="aa";
        try {
        SimpleHash oldSh=new SimpleHash("MD5",oldPwd,salt,1024);
        SimpleHash newSh=new SimpleHash("MD5",newPwd,salt,1024);
            if (oldSh.toHex().equals(user.getPwd())) {
                user.setPwd(newSh.toHex());
                System.out.println("ce");
                userServcie.updateUser(user);
                map.put("result", true);
            }
            }catch(Exception e){
                e.printStackTrace();
                map.put("result", false);
             }
        System.out.println(map.get("result"));
        return map;
    }





}
